package org.esprit.ocm.server.metier.oca.impl;

import java.util.ArrayList;
import java.util.List;

import org.esprit.ocm.client.rpc.oca.VMOcaService;
import org.esprit.ocm.model.oca.VmOca;
import org.esprit.ocm.server.metier.oca.api.Client;
import org.esprit.ocm.server.metier.oca.api.OneResponse;
import org.esprit.ocm.server.metier.oca.api.vm.VirtualMachine;
import org.esprit.ocm.server.metier.oca.api.vm.VirtualMachinePool;

public class VMOcaServiceImpl implements VMOcaService {

	/**
	 * List All vms
	 * 
	 * @param client
	 * @return
	 * @throws Exception
	 */
	public List<VmOca> listAll(String secret, String endpoint, int filter) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualMachinePool pool = new VirtualMachinePool(client, filter);
		pool.info();

		List<VmOca> l = new ArrayList<VmOca>();

		for (VirtualMachine from : pool) {
			VmOca to = new VmOca();
			to.setId(from.xpath("ID"));
			to.setUid(from.xpath("UID"));
			to.setUsername(from.xpath("USERNAME"));
			to.setName(from.xpath("NAME"));
			to.setLast_poll(from.xpath("LAST_POLL"));
			to.setState(from.xpath("STATE"));
			to.setLcm_state(from.xpath("LCM_STATE"));
			to.setStime(from.xpath("STIME"));
			to.setEtime(from.xpath("ETIME"));
			to.setDeploy_id(from.xpath("DEPLOY_ID"));
			to.setMemory(from.xpath("MEMORY"));
			to.setCpu(from.xpath("CPU"));
			to.setNet_tx(from.xpath("NET_TX"));
			to.setNet_rx(from.xpath("NET_RX"));
			to.setTemplate_cpu(from.xpath("TEMPLATE/CPU"));
			to.setTemplate_memory(from.xpath("TEMPLATE/MEMORY"));
			to.setTemplate_name(from.xpath("TEMPLATE/NAME"));
			to.setTemplate_vmid(from.xpath("TEMPLATE/VMID"));
			to.setTemplate_disk_bus(from.xpath("TEMPLATE/DISK/BUS"));
			to.setTemplate_disk_clone(from.xpath("TEMPLATE/DISK/CLONE"));
			to.setTemplate_disk_disk_id(from.xpath("TEMPLATE/DISK/DISK_ID"));
			to.setTemplate_disk_image(from.xpath("TEMPLATE/DISK/IMAGE"));
			to.setTemplate_disk_image_id(from.xpath("TEMPLATE/DISK/IMAGE_ID"));
			to.setTemplate_disk_readonly(from.xpath("TEMPLATE/DISK/READONLY"));
			to.setTemplate_disk_save(from.xpath("TEMPLATE/DISK/SAVE"));
			to.setTemplate_disk_source(from.xpath("TEMPLATE/DISK/SOURCE"));
			to.setTemplate_disk_target(from.xpath("TEMPLATE/DISK/TARGET"));
			to.setTemplate_disk_type(from.xpath("TEMPLATE/DISK/TYPE"));
			to.setTemplate_os_arch(from.xpath("TEMPLATE/OS"));
			to.setTemplate_os_boot(from.xpath("TEMPLATE/OS"));
			to.setHistory_seq(from.xpath("HISTORY/SEQ"));
			to.setHistory_hostname(from.xpath("HISTORY/HOSTNAME"));
			to.setHistory_vm_dir(from.xpath("HISTORY/VM_DIR"));
			to.setHistory_hid(from.xpath("HISTORY/HID"));
			to.setHistory_stime(from.xpath("HISTORY/STIME"));
			to.setHistory_etime(from.xpath("HISTORY/ETIME"));
			to.setHistory_vmmmad(from.xpath("HISTORY/VMMMAD"));
			to.setHistory_tmmad(from.xpath("HISTORY/TMMAD"));
			to.setHistory_pstime(from.xpath("HISTORY/PSTIME"));
			to.setHistory_petime(from.xpath("HISTORY/PETIME"));
			to.setHistory_rstime(from.xpath("HISTORY/RSTIME"));
			to.setHistory_retime(from.xpath("HISTORY/RETIME"));
			to.setHistory_estime(from.xpath("HISTORY/ESTIME"));
			to.setHistory_eetime(from.xpath("HISTORY/EETIME"));
			to.setHistory_reason(from.xpath("HISTORY/REASON"));

			to.setErrorMSG(from.info().getErrorMessage());
			l.add(to);
		}
		return l;
	}

	/**
	 * Describe vm information
	 * 
	 * @param client
	 * @param vnetId
	 * @return
	 * @throws Exception
	 */
	public VmOca info(String secret, String endpoint, int vmId) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualMachine from = new VirtualMachine(vmId, client);

		VmOca to = new VmOca();
		to.setId(from.xpath("ID"));
		to.setUid(from.xpath("UID"));
		to.setUsername(from.xpath("USERNAME"));
		to.setName(from.xpath("NAME"));
		to.setLast_poll(from.xpath("LAST_POLL"));
		to.setState(from.xpath("STATE"));
		to.setLcm_state(from.xpath("LCM_STATE"));
		to.setStime(from.xpath("STIME"));
		to.setEtime(from.xpath("ETIME"));
		to.setDeploy_id(from.xpath("DEPLOY_ID"));
		to.setMemory(from.xpath("MEMORY"));
		to.setCpu(from.xpath("CPU"));
		to.setNet_tx(from.xpath("NET_TX"));
		to.setNet_rx(from.xpath("NET_RX"));
		to.setTemplate_cpu(from.xpath("TEMPLATE/CPU"));
		to.setTemplate_memory(from.xpath("TEMPLATE/MEMORY"));
		to.setTemplate_name(from.xpath("TEMPLATE/NAME"));
		to.setTemplate_vmid(from.xpath("TEMPLATE/VMID"));
		to.setTemplate_disk_bus(from.xpath("TEMPLATE/DISK/BUS"));
		to.setTemplate_disk_clone(from.xpath("TEMPLATE/DISK/CLONE"));
		to.setTemplate_disk_disk_id(from.xpath("TEMPLATE/DISK/DISK_ID"));
		to.setTemplate_disk_image(from.xpath("TEMPLATE/DISK/IMAGE"));
		to.setTemplate_disk_image_id(from.xpath("TEMPLATE/DISK/IMAGE_ID"));
		to.setTemplate_disk_readonly(from.xpath("TEMPLATE/DISK/READONLY"));
		to.setTemplate_disk_save(from.xpath("TEMPLATE/DISK/SAVE"));
		to.setTemplate_disk_source(from.xpath("TEMPLATE/DISK/SOURCE"));
		to.setTemplate_disk_target(from.xpath("TEMPLATE/DISK/TARGET"));
		to.setTemplate_disk_type(from.xpath("TEMPLATE/DISK/TYPE"));
		to.setTemplate_os_arch(from.xpath("TEMPLATE/OS"));
		to.setTemplate_os_boot(from.xpath("TEMPLATE/OS"));
		to.setHistory_seq(from.xpath("HISTORY/SEQ"));
		to.setHistory_hostname(from.xpath("HISTORY/HOSTNAME"));
		to.setHistory_vm_dir(from.xpath("HISTORY/VM_DIR"));
		to.setHistory_hid(from.xpath("HISTORY/HID"));
		to.setHistory_stime(from.xpath("HISTORY/STIME"));
		to.setHistory_etime(from.xpath("HISTORY/ETIME"));
		to.setHistory_vmmmad(from.xpath("HISTORY/VMMMAD"));
		to.setHistory_tmmad(from.xpath("HISTORY/TMMAD"));
		to.setHistory_pstime(from.xpath("HISTORY/PSTIME"));
		to.setHistory_petime(from.xpath("HISTORY/PETIME"));
		to.setHistory_rstime(from.xpath("HISTORY/RSTIME"));
		to.setHistory_retime(from.xpath("HISTORY/RETIME"));
		to.setHistory_estime(from.xpath("HISTORY/ESTIME"));
		to.setHistory_eetime(from.xpath("HISTORY/EETIME"));
		to.setHistory_reason(from.xpath("HISTORY/REASON"));

		to.setErrorMSG(from.info().getErrorMessage());
		return to;
	}

	/**
	 * Create New VM
	 * 
	 * @param client
	 * @param template
	 * @param image
	 * @return
	 * @throws Exception
	 */
	public String allocate(String secret, String endpoint, String template) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = VirtualMachine.allocate(client, template);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}
	
	/**
	 * Delete VM
	 * 
	 * @param client
	 * @param imgId
	 * @throws Exception
	 */
	public String delete(String secret, String endpoint, int vmId) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualMachine vm = new VirtualMachine(vmId, client);
		OneResponse res = vm.finalizeVM();
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * 
	 * @param client
	 * @param vmId
	 * @throws Exception
	 */
	public String hold(String secret, String endpoint, int vmId) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualMachine vm = new VirtualMachine(vmId, client);
		OneResponse res = vm.hold();
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * 
	 * @param client
	 * @param vmId
	 * @throws Exception
	 */
	public String release(String secret, String endpoint, int vmId) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualMachine vm = new VirtualMachine(vmId, client);
		OneResponse res = vm.release();
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * 
	 * @param client
	 * @param vmId
	 * @param hostId
	 * @throws Exception
	 */
	public String deploy(String secret, String endpoint, int vmId, int hostId) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualMachine vm = new VirtualMachine(vmId, client);
		OneResponse res = vm.deploy(hostId);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * 
	 * @param client
	 * @param vmId
	 * @param hostId
	 * @throws Exception
	 */
	public String migrate(String secret, String endpoint, int vmId, int hostId) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualMachine vm = new VirtualMachine(vmId, client);
		OneResponse res = vm.migrate(hostId);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * 
	 * @param client
	 * @param vmId
	 * @param hostId
	 * @throws Exception
	 */
	public String liveMigrate(String secret, String endpoint, int vmId, int hostId) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualMachine vm = new VirtualMachine(vmId, client);
		OneResponse res = vm.liveMigrate(hostId);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * 
	 * @param client
	 * @param vmId
	 * @throws Exception
	 */
	public String shutdown(String secret, String endpoint, int vmId) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualMachine vm = new VirtualMachine(vmId, client);
		OneResponse res = vm.shutdown();
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * 
	 * @param client
	 * @param vmId
	 * @throws Exception
	 */
	public String cancel(String secret, String endpoint, int vmId) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualMachine vm = new VirtualMachine(vmId, client);
		OneResponse res = vm.cancel();
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * 
	 * @param client
	 * @param vmId
	 * @throws Exception
	 */
	public String stop(String secret, String endpoint, int vmId) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualMachine vm = new VirtualMachine(vmId, client);
		OneResponse res = vm.stop();
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * 
	 * @param client
	 * @param vmId
	 * @throws Exception
	 */
	public String suspend(String secret, String endpoint, int vmId) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualMachine vm = new VirtualMachine(vmId, client);
		OneResponse res = vm.suspend();
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * 
	 * @param client
	 * @param vmId
	 * @throws Exception
	 */
	public String resume(String secret, String endpoint, int vmId) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualMachine vm = new VirtualMachine(vmId, client);
		OneResponse res = vm.resume();
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * 
	 * @param client
	 * @param vmId
	 * @throws Exception
	 */
	public String finalize(String secret, String endpoint, int vmId) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualMachine vm = new VirtualMachine(vmId, client);
		OneResponse res = vm.finalizeVM();
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * 
	 * @param client
	 * @param vmId
	 * @throws Exception
	 */
	public String restart(String secret, String endpoint, int vmId) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualMachine vm = new VirtualMachine(vmId, client);
		OneResponse res = vm.restart();
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

}
